package booroo.swing;
import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JRadioButton;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import java.awt.Dimension;

import javax.swing.LayoutStyle.ComponentPlacement;

import booroo.dao.MemberDAO;
import booroo.model.MemberVO;
import booroo.model.Member_V0;


import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.ObjectInputStream.GetField;


public class UpdateMember extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JPasswordField pwd;
	private JTextField uid;
	private JTextField name;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			UpdateMember dialog = new UpdateMember("0", 0, null);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public UpdateMember(final String mno, final int r, final JTable table) {
		// 회원정보수정을 위해 ManageMember에서 전달한 mno를 받기위해
		// UpdateMember의 기본생성자 대신 mnp를 매개변수로 하는 새로운 생성자 작성
		setTitle("회원수정");
		setBounds(100, 100, 640, 480);
		setLocationRelativeTo(null);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.NORTH);
		
		JLabel txtGender = new JLabel("성별");
		txtGender.setFont(new Font("굴림", Font.BOLD, 14));
		
		JLabel txtPwd = new JLabel("비밀번호");
		txtPwd.setFont(new Font("굴림", Font.BOLD, 14));
		
		JLabel txtUid = new JLabel("아이디");
		txtUid.setFont(new Font("굴림", Font.BOLD, 14));
		
		JLabel txtName = new JLabel("이름");
		txtName.setFont(new Font("굴림", Font.BOLD, 14));
		
		JRadioButton isMale = new JRadioButton("남자");
		isMale.setFont(new Font("굴림", Font.BOLD, 14));
		
		JRadioButton isFemale = new JRadioButton("여자");
		isFemale.setSelected(true);
		isFemale.setFont(new Font("굴림", Font.BOLD, 14));
		
		pwd = new JPasswordField();
		
		uid = new JTextField();
		uid.setEnabled(false);
		uid.setPreferredSize(new Dimension(7, 21));
		uid.setColumns(10);
		
		name = new JTextField();
		name.setColumns(10);
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(37)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(txtPwd, GroupLayout.PREFERRED_SIZE, 80, GroupLayout.PREFERRED_SIZE)
						.addComponent(txtName, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
						.addComponent(txtUid)
						.addComponent(txtGender, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
					.addGap(31)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
							.addComponent(name, GroupLayout.PREFERRED_SIZE, 191, GroupLayout.PREFERRED_SIZE)
							.addComponent(uid, GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE))
						.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING, false)
							.addGroup(Alignment.TRAILING, gl_contentPanel.createSequentialGroup()
								.addComponent(isMale, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
								.addComponent(isFemale, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE))
							.addComponent(pwd, GroupLayout.PREFERRED_SIZE, 191, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(275, Short.MAX_VALUE))
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(30)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(1)
							.addComponent(txtName, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE))
						.addComponent(name, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(uid, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addGap(7))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(txtUid, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.UNRELATED)))
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGap(1)
							.addComponent(txtPwd, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE))
						.addComponent(pwd, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addComponent(isFemale, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
							.addComponent(isMale, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
							.addComponent(txtGender, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(253, Short.MAX_VALUE))
		);
		contentPanel.setLayout(gl_contentPanel);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okBtn = new JButton("수정완료");
				okBtn.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						MemberVO m = new MemberVO(mno,name.getText(),null,new String(pwd.getPassword()),null,null);
						int cnt = new MemberDAO().updateMember(m);
						if(cnt > 0){
							JOptionPane.showMessageDialog(null, "수정완료!");
							table.setValueAt(name.getText(), r, 2);
							table.repaint();
							dispose();
						
							
						}
														
					}
				});
				okBtn.setActionCommand("OK");
				buttonPane.add(okBtn);
				getRootPane().setDefaultButton(okBtn);
			}
			{
				JButton notBtn = new JButton("수정취소");
				notBtn.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						dispose();
					}
				});
				notBtn.setActionCommand("Cancel");
				buttonPane.add(notBtn);
			}
		}
		
		// 회원번호를 이용해서 특정회원의 모든 정보를 select한후 
		// MemberVO 에 저장
		MemberVO m = new MemberDAO().selectOneMember(mno);
		// MemberVO의 값을 컴퍼넌트에 출력
		uid.setText(m.getUserid());
		name.setText(m.getName());
		pwd.setText(m.getPwd());
	} // 생성자

	
}
